﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace Botwave.Log
{
    /// <summary>
    ///LogData 的摘要说明
    /// </summary>
    public static class LogData
    {

        private static IDictionary<string, string> Operation;
        private static IDictionary<string, string> Exception;
        private readonly static string ConnectionString;

        static LogData()
        {
            ConnectionString = ConfigurationManager.AppSettings.Get("ConnectionString");
            AddOperationDictionary();
            AddExceptionDictionary();
        }

        public static string GetOperationId(string key)
        {
            return Operation.ContainsKey(key) ? Operation[key] : "0";
        }
        public static string GetExceptionId(string key)
        {
            return Exception.ContainsKey(key) ? Exception[key] : "0";
        }


        /// <summary>
        /// AddOperation2Dictionary
        /// </summary>
        /// <returns></returns>
        private static void AddOperationDictionary()
        {

            Operation = new Dictionary<string, string>();
            string sql = "select operationName, operationID from TB_Log_OperationCatalog";
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                IDbCommand dbCmd = conn.CreateCommand();
                dbCmd.CommandText = sql;
                IDataReader reader = dbCmd.ExecuteReader();
                while (reader.Read())
                {
                    if (!Operation.ContainsKey(reader.GetString(0)))
                        Operation.Add(reader.GetString(0), reader.GetString(1));
                } 
            }
        }

        /// <summary>
        /// AddException2Dictionary
        /// </summary>
        /// <returns></returns>
        private static void AddExceptionDictionary()
        {
            Exception = new Dictionary<string, string>();
            string sql = "select exceptionName, exceptionID from TB_Log_ExceptionCatalog";
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                IDbCommand dbCmd = conn.CreateCommand();
                dbCmd.CommandText = sql;
                IDataReader reader = dbCmd.ExecuteReader();
                while (reader.Read())
                {
                    if (!Exception.ContainsKey(reader.GetString(0)))
                        Exception.Add(reader.GetString(0), reader.GetString(1));
                }
            }
        }

    }
}
